package com.tb.Service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tb.DTO.scheduleDTO;
import com.tb.DTO.schedulePageQueryDTO;
import com.tb.Service.TbService;

import com.tb.entity.schedule;
import com.tb.mapper.TbMapper;
import com.tb.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Component
public class TbServiceImpl implements TbService {
    @Autowired
    private TbMapper tbMapper;  // 确保是非静态的


    public void add(scheduleDTO scheduleDTO) {
        // 调用 Mapper 插入到数据库
        tbMapper.add(scheduleDTO.getTitle(), scheduleDTO.getType(), scheduleDTO.getStartTime(), scheduleDTO.getEndTime(), scheduleDTO.getPriority(), scheduleDTO.getStatus());
    }


    public Object get(Long id) {
        schedule schedule = tbMapper.get(id);
        System.out.println(schedule);
        return schedule;
    }


    public void update(scheduleDTO scheduleDTO) {
        //检验时间输入的格式是否正确
        if (scheduleDTO.getStartTime() != null && scheduleDTO.getEndTime() != null) {
            // 调用 Mapper 方法执行更新操作
            tbMapper.updateSchedule(scheduleDTO);
        } else {
            throw new IllegalArgumentException("时间格式不正确");
        }
    }

    private LocalDateTime parseDateTime(String dateTimeStr) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateTimeStr, formatter);
    }


    private boolean isValidTimeFormat(String dateTime) {

        // 尝试解析 "yyyy-MM-dd HH:mm:ss" 格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime.parse(dateTime, formatter);
        return true;
    }


    public boolean deleteByOne(Long id) {
        return tbMapper.deleteScheduleById(id) > 0;
    }

    //批量删除
    public boolean deleteByBatch(Long[] ids) {
        return tbMapper.deleteScheduleByBatch(ids) == ids.length;
    }
//修改日程的状态

    public boolean updateStutes(Long id, Integer status) {
        if (status == null || status < 0 || status > 3) {
            return false;
        }
        int rowsAffected = tbMapper.updateStatus(id, status);
        return rowsAffected > 0;
    }

    //分页查询
    public PageResult pageQuery(Integer page,
                                Integer pageSize,
                                String title,
                                Integer type,
                                Integer priority,
                                Integer status,
                                LocalDate startTime,
                                LocalDate endTime) {
        PageHelper.startPage(page, pageSize);
        Page<schedule> p = tbMapper.pageQuery(title, type, priority, status, startTime, endTime);
        long total = p.getTotal();
        List<schedule> result = p.getResult();
        return new PageResult(total, result);
    }


}
